/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import lejos.nxt.Motor;

/**
 *
 * @author damy3n
 */
public class Engine
{

    private Motor left = Motor.A;
    private Motor right = Motor.C;
    private int speed;
    private final int FULLSPEED = 900;
    private boolean rotateRight;

    /**
     * Creates an object
     * @param speed
     */
    public Engine(int speed)
    {
        this.speed = speed;

    }

    /**
     * Sets if last rotation was wether in the right direction or not
     * @param rotateRight
     */
    public void setRotateRight(boolean rotateRight)
    {
        this.rotateRight = rotateRight;
    }

    /**
     *
     * @return true if last rotation was in the right direction
     */
    public boolean isRotateRight()
    {
        return rotateRight;
    }

    /**
     * Engines drive forward
     */
    public void forward()
    {
        setSpeed(speed);
        left.forward();
        right.forward();

    }

    /**
     * Engines drive backward
     */
    public void backward()
    {
        setSpeed(speed);
        left.backward();
        right.backward();
    }

    /**
     *Chewie rotates left
     */
    public void rotateLeft()
    {
        //setSpeed(FULLSPEED);
        left.backward();
        right.forward();
    }

    /**
     * Chewie rotates right
     */
    public void rotateRight()
    {
        //setSpeed(FULLSPEED);
        left.forward();
        right.backward();
    }

    /**
     * Drives a left turn
     * @param prozent percentage of the left motor speed
     */
    public void driveLeft(int prozent)
    {
        setLeftSpeed((speed * prozent) / 100);
        left.forward();
        right.forward();
    }

    /**
     * Drives right turn
     * @param prozent sets the percentage of the right motor speed
     */
    public void driveRight(int prozent)
    {
        setRightSpeed((speed * prozent) / 100);
        left.forward();
        right.forward();
    }

    /**
     * Sets temporary the speed
     * @param speed
     */
    public void setSpeed(int speed)
    {
        //this.speed=speed;
        left.setSpeed(speed);
        right.setSpeed(speed);
    }

    private void setLeftSpeed(int speed)
    {
        left.setSpeed(speed);
    }

    private void setRightSpeed(int speed)
    {
        right.setSpeed(speed);
    }

    /**
     *
     * @return speed of the left motor
     */
    public int getLeftSpeed()
    {
        return left.getSpeed();
    }

    /**
     *
     * @return speed of the left motor
     */
    public int getRightSpeed()
    {
        return right.getSpeed();
    }

    /**
     * stop both motors
     */
    public void stop()
    {
        left.stop();
        right.stop();
    }

    /**
     * switchs the rotation
     */
    public void switchRotation()
    {
        if (isRotateRight())
        {
            setRotateRight(false);
            rotateLeft();

        } else
        {
            setRotateRight(true);
            rotateRight();
        }
    }

    /**
     *Turns around
     */
    public void rotate180()
    {
        setSpeed(600);
        left.rotate(800, true);
        right.rotate(-800, false);
    }
}
